package _1_Basic;

import org.junit.Test;

import java.lang.reflect.Method;

/**
 * Created by bpzj on 2019-02-12.
 */
public class AboutException {

    /**
     * Throwable                    {@link java.lang.Throwable}
     *  ├─ Error                    {@link java.lang.Error}
     *  └─ Exception                {@link java.lang.Exception}
     *      ├─ RuntimeException     {@link java.lang.RuntimeException}
     *      └─ 非运行时异常
     */
    /*                                     Throwable 所有异常的基类
                                                   ↑
                       ┌───────────────────────────┴───────────────────────────┐
                     Error                                                  Exception
                       ↑                                                       ↑
       OutOfMemoryError, NoSuchMethodError, ...               ┌────────────────┴─────────────────┐
                                                       RuntimeException                IOException, ClassNotFoundException
                                                              ↑                           等, 必须 catch或 throw
                                           ClassCastException, NullPointerException
                                           IndexOutOfBoundsException

     */
    @Test
    public void catchWhich() throws Throwable {
        try {
            Class<?> hello = Class.forName("java.lang.String");
            Method method = hello.getMethod("method");
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
            throw new Error();
        }
    }

    /**
     * 即使是在 catch (Exception e) {} 中, 写了 return, finally 也会执行
     */
    @Test
    public void testFinal() {
        try {
            String a = null;
            boolean aNull = a.equals("null");
        } catch (Exception e) {
            e.printStackTrace();
            return;
        } finally {
            System.out.println("finally 语句");
        }
    }

}
